/**
 * Creat with IntelliJ IDEA
 * Description:interrupt()方法
 * 当一些线程调用sleep()方法处于休眠状态时，
 * 一个使用CPU资源的其它线程在执行过程中，可以让休眠的线程分别调用interrupt()方法“吵醒”自己，
 * 即导致休眠的线程发生InterruptedException异常，从而结束休眠，重新排队等待CPU资源。
 * User:WZW
 * Date:2021-11-26
 * Time:21:43
 */
class ClassRoom implements Runnable{
    Thread teacher, zhang, li;
    ClassRoom(){
        teacher = new Thread(this);
        zhang = new Thread(this);
        li = new Thread(this);
        zhang.setName("Zhang");
        li.setName("Li");
        teacher.setName("Pan");
    }

    public void run(){
        Thread thread = Thread.currentThread();
        if(thread==zhang || thread==li){
            try{  System.out.println(thread.getName() + ": Sleep for 10s");
                Thread.sleep(10000);
            }
            catch(InterruptedException e){
                System.out.println(thread.getName() + ": been wake up");
            }
            System.out.println(thread.getName() + ": Good morning");
        }
        else if(thread==teacher){
            for(int i=1;i<=3;i++){
                System.out.println(thread.getName() + ": Let's start ...");
                try{
                    Thread.sleep(500);
                }
                catch(InterruptedException e) {}
            }
            zhang.interrupt();
            li.interrupt();
        }
    }
}

public class Example8_8 {
    public static void main(String args[])
    {
        ClassRoom room = new ClassRoom();
        room.zhang.start();
        room.li.start();
        room.teacher.start();
    }

}
